package sd.czw.com.mmgapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.activity.GoodsDeatilActivity;
import sd.czw.com.mmgapp.model.categorybean.CategoryFragmenGrideViewtInfo;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/7.
 */
public class CategoryGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int HEADER_TYPE = 0;
    public static final int VIEW_TYPE = 1;

    private Context context;
    private List<CategoryFragmenGrideViewtInfo.ErrmsgBean.CatGoodsBean> datas;
    private String path;//图片url
    private int width;

    public CategoryGoodsAdapter(Context context, int width,String path,List<CategoryFragmenGrideViewtInfo.ErrmsgBean.CatGoodsBean> datas) {
        this.context = context;
        this.width = width;
        this.path = path;
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (HEADER_TYPE == viewType) {
            return new HeaderViewHolder(LayoutInflater.from(context).inflate(R.layout.header_image, null));
        } else if (VIEW_TYPE == viewType){
            return new GoodsViewHolder(LayoutInflater.from(context).inflate(R.layout.syrv_child, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)){
            case HEADER_TYPE:
                Glide.with(context).load(path).asBitmap().into(((HeaderViewHolder)holder).headerimage);//下载图片\
//                ((HeaderViewHolder)holder).headerimage.setLayoutParams(new LinearLayout.LayoutParams(width,(int)(width/Double.valueOf(2.5))));
//                ((HeaderViewHolder)holder).headerimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                break;
            case VIEW_TYPE:
                if (datas != null){
                    Glide.with(context).load(Contants.BASE_URL+datas.get(position-1).getGoods_img()).placeholder(new ColorDrawable(Color.parseColor("#f5f5f5"))).into(((GoodsViewHolder) holder).layoutGvChildImg);
                    ((GoodsViewHolder) holder).layoutGvChildImg.setLayoutParams(new LinearLayout.LayoutParams((int)(width/Double.valueOf(2)),(int)(width/Double.valueOf(2))));
                    ((GoodsViewHolder) holder).layoutGvChildTvtitle.setText(datas.get(position-1).getGoods_name());
                    ((GoodsViewHolder) holder).layoutGvChildTvprice1.setText("¥"+datas.get(position-1).getShop_price());
                    ((GoodsViewHolder) holder).layoutGvChildTvprice2.setText("¥"+datas.get(position-1).getMarket_price());
                    ((GoodsViewHolder) holder).layoutGvChildTvprice2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    ((GoodsViewHolder) holder).layoutGvChildTvnum.setText(datas.get(position-1).getClick_count()+"人浏览");
                    ((GoodsViewHolder) holder).layoutGvChildCount2.setText(""+datas.get(position-1).getSales_count());
                    ((GoodsViewHolder) holder).itemView.setFocusable(true);
                    ((GoodsViewHolder) holder).layoutGvChildImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, GoodsDeatilActivity.class);
                            intent.putExtra("goods_id",datas.get(position-1).getGoods_id());
                            context.startActivity(intent);
                        }
                    });
                    ((GoodsViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, GoodsDeatilActivity.class);
                            intent.putExtra("goods_id",datas.get(position-1).getGoods_id());
                            context.startActivity(intent);
                        }
                    });
                }
                break;
        }
    }

    @Override
    public int getItemCount() {
       return datas.size()+1;
    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == HEADER_TYPE
                            ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return HEADER_TYPE;
        }else {
            return VIEW_TYPE;
        }
    }
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.header_image)
        ImageView headerimage;
        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    class GoodsViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.layout_gv_child_img)
        ImageView layoutGvChildImg;
        @Bind(R.id.layout_gv_child_tvtitle)
        TextView layoutGvChildTvtitle;
        @Bind(R.id.layout_gv_child_tvprice1)
        TextView layoutGvChildTvprice1;
        @Bind(R.id.layout_gv_child_tvprice2)
        TextView layoutGvChildTvprice2;
        @Bind(R.id.layout_gv_child_count1)
        TextView layoutGvChildCount1;
        @Bind(R.id.layout_gv_child_count2)
        TextView layoutGvChildCount2;
        @Bind(R.id.layout_gv_child_tvnum)
        TextView layoutGvChildTvnum;

        public GoodsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
